package com.tj.api.task;

import com.fqgj.common.utils.CollectionUtils;
import com.tj.bo.account.server.dao.OrderDepositDao;
import com.tj.bo.common.conf.config.RedisConfigUtil;
import com.tj.bo.common.conf.enums.UserTypeEnum;
import com.tj.bo.trade.server.dao.BinaryTradeDao;
import com.tj.bo.user.server.dao.UserAccessDao;
import com.tj.bo.user.server.dao.UserWanningListDao;
import com.tj.bo.user.server.entity.UserAccessEntity;
import com.tj.bo.user.server.entity.UserWanningListEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @program: binary-option
 * @description 加入黑名单任务
 * @author: frank
 * @create: 2020-09-22 10:29
 **/
@Component
public class AddBlackUserTask {
    private static final Logger LOGGER = LoggerFactory.getLogger(AddBlackUserTask.class);

    @Autowired
    private UserAccessDao userAccessDao;
    @Autowired
    private BinaryTradeDao binaryTradeDao;
    @Autowired
    private OrderDepositDao orderDepositDao;
    @Autowired
    private UserWanningListDao userWanningListDao;
    @Autowired
    private RedisConfigUtil redisConfigUtil;

    /**
     * 加入黑名单任务
     */
    //@Scheduled(cron="0 0/10 * * * ?")
    public void addBlackUserTask(){
        List<UserAccessEntity> userAccessEntities = userAccessDao.selectRealUserAccessGroupByDeviceId(redisConfigUtil.deviceIdNumber());
        if(CollectionUtils.isNotEmpty(userAccessEntities)){
            for(UserAccessEntity entity : userAccessEntities){
                BigDecimal totalProfitAmount = binaryTradeDao.calculateUserTotalProfitAmount(null, entity.getUserCode());
                BigDecimal totalDepositAmount = orderDepositDao.selectTotalDepositAmount(entity.getUserCode());
                if(BigDecimal.ZERO.compareTo(totalDepositAmount) == 0){
                    continue;
                }
                if((totalProfitAmount.divide(totalDepositAmount).setScale(2, BigDecimal.ROUND_HALF_UP).compareTo(new BigDecimal(2)) >= 0)){//盈利两倍且deviceId大于等于3加入黑名单
                    List<UserAccessEntity> userAccessEntityList = userAccessDao.selectUserAccessInfoByDeviceId(entity.getDeviceId());
                    if(CollectionUtils.isNotEmpty(userAccessEntityList)){
                        for(UserAccessEntity userAccessEntity : userAccessEntityList){
                            UserWanningListEntity userWanningListEntity = new UserWanningListEntity();
                            userWanningListEntity.setUserCode(userAccessEntity.getUserCode());
                            userWanningListEntity.setDeviceId(userAccessEntity.getDeviceId());
                            userWanningListEntity.setType(UserTypeEnum.BLACK.getCode());
                            userWanningListDao.insert(userWanningListEntity);
                        }
                    }
                }
            }
            LOGGER.info("============ 黑名单任务执行SUCCESS ==========");
        }
    }
}
